package com.todo.event.activity.application.util;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.todo.common.utils.StringUtils;
import com.todo.component.hbase.service.HBaseService;
import com.todo.event.activity.application.entity.model.EventHbaseDataModel;
import com.todo.event.activity.common.base.EventKeyBuild;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author azhebuxing
 * @date 2025/1/10 12:46
 * @description
 */
@Slf4j
@Component
public class EventHBaseUtil {

    @Resource
    private HBaseService hBaseService;


    public boolean insertEventData(EventHbaseDataModel eventHbaseDataModel) {
        String tableName = EventKeyBuild.getEventActivityHbaseTableName();
        String familyName = EventKeyBuild.getEventActivityHbaseFamilyName();
        String rowKey = EventKeyBuild.buildEventActivityHbaseRowKey(eventHbaseDataModel.getUserId(), eventHbaseDataModel.getUk());
        String column = String.valueOf(eventHbaseDataModel.getDecisionId());
        hBaseService.putData(tableName, rowKey, familyName, column, eventHbaseDataModel.getSaveValue().toJSONString(), eventHbaseDataModel.getTtl());
        return true;
    }

    public List<EventHbaseDataModel> queryEventDataListByUserId(Long userId, Long decisionId) {

        String tableName = EventKeyBuild.getEventActivityHbaseTableName();
        String prefix = EventKeyBuild.buildEventActivityHbasePrefixRowKey(userId);
        String column = String.valueOf(decisionId);
        Map<String, Map<String, String>> filter = hBaseService.getResultScannerByColumnAndRowPrefixFilter(tableName, column, prefix);
        if (MapUtils.isEmpty(filter)) {
            return Lists.newArrayList();
        }
        List<EventHbaseDataModel> resultList = Lists.newArrayList();
        for (String key : filter.keySet()) {
            Map<String, String> stringStringMap = filter.get(key);
            if (MapUtils.isEmpty(stringStringMap)) {
                continue;
            }
            List<String> strings = StringUtils.splitList(key, "_");
            EventHbaseDataModel model = new EventHbaseDataModel();
            model.setUserId(Long.valueOf(strings.get(0)));
            model.setUk(strings.get(1));
            model.setDecisionId(decisionId);
            model.setSaveValue(JSONObject.parseObject(stringStringMap.get(column)));
            resultList.add(model);
        }
        return resultList;
    }

    public Boolean removeEventData(EventHbaseDataModel eventHbaseDataModel) {
        String tableName = EventKeyBuild.getEventActivityHbaseTableName();
        String familyName = EventKeyBuild.getEventActivityHbaseFamilyName();
        String rowKey = EventKeyBuild.buildEventActivityHbaseRowKey(eventHbaseDataModel.getUserId(), eventHbaseDataModel.getUk());
        String column = String.valueOf(eventHbaseDataModel.getDecisionId());
        boolean b = hBaseService.deleteColumn(tableName, rowKey, familyName, column);
        return b;
    }
}
